<?php

class UsersAction extends Action
{
    public function index() {

    	import('ORG.Util.Page');

    	$U = M('user_base_info');
    	$count = $U->count();
    	$Page = new Page( $count , 15 );
    	$show = $Page->show();
    	$list = $U->limit( $Page->firstRow . ',' . $Page->listRows )->select();

    	$this->assign('list' , $list);
    	$this->assign('page' , $show);
        $this->display();
    }

    public function usersDel(){

    	$U = M("user_base_info");
    	$sessionName = $_SESSION['userName'];
    	$userInfo = $U->where("user_name='$sessionName'")->getField('user_name');

    	if( !empty($userInfo) ) {
    		$this->assign('jumpUrl' , "__URL__/index");
    		$this->error('当前用户正在登录不能删除');
    	}else{
    		$userCode = $_GET['user_code'];
    		$U->where("user_code=$userCode")->delete();
    		$this->assign('jumpUrl' , "__URL__/index");
    		$this->success('删除成功');
    	}
    }

    public function userAdd(){

        $U = M("province");
        $proList = $U->select();

        $proCode = $_GET['proCode'];
        if( !empty($proCode) ){
            $U = M('city');
            $cityList = $U->where("pro_code=$proCode")->select();
        }else{
            $U = M('city');
            $cityList = $U->where("pro_code=1")->select();
        }

        $this->assign('proCode' , $proCode);
        $this->assign('citylist' , $cityList);
        $this->assign('prolist' , $proList);
        $this->display('useradd');
    }

    public function editPass(){

    	$userPass = md5($_POST['user_passwd']);
    	$userNewPass = $_POST['user_new_passwd'];
    	$U = M('user_base_info');

        if( $_POST['edit'] ){
        	$userName = $_SESSION['userName'];
        	$userCode = $U->where("user_name='$userName' and user_passwd='$userPass'")->getField('user_code');

            if( empty($userCode) ){
                $this->assign("jumpUrl","__URL__/editPass");
                $this->error('数据更新失败，可能您的旧密码输入错误！');
                exit;
            }

    		if( !empty($userNewPass) ){
        		if($vo = $U->create()){
        			$data['user_passwd'] = md5($userNewPass);
        			$data['user_code'] = $userCode;
        			$result = $U->save($data);
        		}

            	if( $result !== fasle ){
            		 $this->assign("jumpUrl","__URL__/index");
                     $this->success('数据更新成功！');
                     exit;
            	}else{
            		 $this->assign("jumpUrl","__URL__/editPass");
                     $this->error('数据更新失败！');
                     exit;
            	}
            }
        }

    	$this->display('editpass');
    }
}
?>